//
//  StreetView.m
//  gong
//
//  Created by echo on 14-2-19.
//  Copyright (c) 2014年 echo. All rights reserved.
//

#import "StreetView.h"
#import "PositionTools.h"
#import "UICreator.h"
#import "GameManager.h"
#import "MusicHouse.h"
#import "PlayerHouse.h"
#import "EmperorMotherHouse.h"

#import "HuaFei.h"
#import "DuanFei.h"
#import "MeiFei.h"

#import "Constants.h"

@interface StreetView () <UIScrollViewDelegate>

@end

@implementation StreetView
{
    UIImageView *_streetView;
    NSTimer *_scrollTimer;
    float _offsetX;
    BOOL _needAutoScroll;
    NSMutableArray *_placeArray;
    UIView *_containerView;
    
    NSMutableArray *_randomNpcArray;
    NSInteger npcIndex;
    
    BOOL _moveToRight;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_scrollTimer invalidate];
    _scrollTimer = nil;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _containerView = [GameManager sharedInstance].gameMainViewContainer;
        
        _streetView = [UICreator createImageViewFromImagename:@"background.jpg"];
        self.contentSize = _streetView.frame.size;
        self.delegate = self;
        [PositionTools placeView:_streetView atTheLeftMiddleOfTheView:self offset:0];
        
        _moveToRight = YES;
        
        [self createHouses];
        [self initStreet];
        [self initRandomNpc];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(stopStreetScroll)];
        [self addGestureRecognizer:tap];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(delayCreateRandomNpc) name:MsgStreetNpcLeave object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(visitNpcHouse) name:MsgVisitNpcHouse object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterMemoryHouse) name:MsgEnterMemoryHouse object:nil];
    }
    return self;
}

- (void)initStreet
{
    [self setContentOffset:CGPointMake(0, 0)];
    
    [_scrollTimer invalidate];
    _needAutoScroll = YES;
    [self performSelector:@selector(beginSrcollStreet) withObject:nil afterDelay:2];
}

- (void)createHouses
{
    _placeArray = [NSMutableArray array];
    
    EmperorMotherHouse *emHouse = [[EmperorMotherHouse alloc] init:_containerView];
    emHouse.streetView = _streetView;
    [emHouse createSmallImage:CGPointMake(6070, 300)];
    
    PlayerHouse *pHouse = [[PlayerHouse alloc] init:_containerView];
    pHouse.streetView = _streetView;
    [pHouse createSmallImage:CGPointMake(760, 400)];
    
    MusicHouse *mHouse = [[MusicHouse alloc] init:_containerView];
    mHouse.streetView = _streetView;
    [mHouse createSmallImage:CGPointMake(1760, 200)];
    
    [_placeArray addObject:emHouse];
    [_placeArray addObject:pHouse];
    [_placeArray addObject:mHouse];
}

- (void)enterMemoryHouse
{
    NSString *houseName = [GameManager sharedInstance].memory.houseName;
    
    for (Place *place in _placeArray) {
        NSLog(@"place des is %@", [place description]);
        if ([[place name] isEqualToString:houseName]) {
            [place clearTransitionInfo];
            [place enter];
        }
    }
}

- (void)visitNpcHouse
{
    PlayerHouse *pHouse = _placeArray[1];
    [pHouse enter];
}

- (void)initRandomNpc
{
    _randomNpcArray = [NSMutableArray array];
    HuaFei *huaFei = [[HuaFei alloc] initWithContainer:_containerView info:nil];
    DuanFei *duanFei = [[DuanFei alloc] initWithContainer:_containerView info:nil];
    MeiFei *meiFei = [[MeiFei alloc] initWithContainer:_containerView info:nil];
    
    [huaFei createStreetAvatar];
    [duanFei createStreetAvatar];
    [meiFei createStreetAvatar];
    
    [_randomNpcArray addObject:huaFei];
    [_randomNpcArray addObject:duanFei];
    [_randomNpcArray addObject:meiFei];
    
    [self delayCreateRandomNpc];
}

- (void)delayCreateRandomNpc
{
    [self performSelector:@selector(createRandomNpc) withObject:nil afterDelay:2];
}

- (void)createRandomNpc
{
    if (npcIndex > _randomNpcArray.count - 1) {
        npcIndex = 0;
    }
    
    NPC *npc = _randomNpcArray[npcIndex];
    npcIndex++;
    
    int randomIndex1 = arc4random() % 20;
    CGFloat npcLeftLimit = 400 + npc.streetAvatar.frame.size.width/2;
    CGFloat posX = npcLeftLimit + ((CGFloat)(self.contentSize.width - npcLeftLimit) / 20.0f) * randomIndex1;
    CGFloat posY = self.frame.size.height - npc.streetAvatar.frame.size.height/2;
    [npc showInStreet:self withPosition:CGPointMake(posX, posY)];
}

- (void)beginSrcollStreet
{
    if (_needAutoScroll) {
        _scrollTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(scrollStreet) userInfo:nil repeats:YES];
    }
}

- (void)scrollStreet
{
    NSInteger moveUnit = 1;
    
    if (_moveToRight) {
        if (_offsetX < _streetView.frame.size.width - 1024) {
            _offsetX += moveUnit;
        } else {
            _moveToRight = NO;
        }
    } else {
        if (_offsetX > 0) {
            _offsetX -= moveUnit;
        } else {
            _moveToRight = YES;
        }
    }
    
    [self setContentOffset:CGPointMake(_offsetX, 0)];
}

- (void)stopStreetScroll
{
    _needAutoScroll = NO;
    if (_scrollTimer) {
        [_scrollTimer invalidate];
        _scrollTimer = nil;
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self stopStreetScroll];
}
@end
